package com.woniuxy.sales.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.sales.mapper.CategoriesMapper;
import com.woniuxy.sales.mapper.ProductsMapper;
import com.woniuxy.sales.model.entity.Categories;
import com.woniuxy.sales.model.entity.Favorites;
import com.woniuxy.sales.model.entity.Products;
import com.woniuxy.sales.model.vo.FavoritesVo;
import com.woniuxy.sales.service.FavoritesService;
import com.woniuxy.sales.mapper.FavoritesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【收藏夹】
*/
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements FavoritesService{
    @Autowired
    private FavoritesMapper favoritesMapper;
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private CategoriesMapper categoriesMapper;

    /**
     * 查询用户的收藏夹
     */
    @Override
    public List<FavoritesVo> query(Integer userId) {
        LambdaQueryWrapper<Favorites> wrapper = new LambdaQueryWrapper<>();
        //查询某个用户的收藏夹
        wrapper.eq(Favorites::getUserId, userId);
        //按加入时间倒序排列
        wrapper.orderByDesc(Favorites::getAddedDate);
        List<Favorites> favorites = favoritesMapper.selectList(wrapper);

        List<FavoritesVo> favoritesVos = favorites.stream().map(fav -> {//每一个收藏的商品
            FavoritesVo favoritesVo = BeanUtil.copyProperties(fav, FavoritesVo.class);
            //根据商品id得到商品详情
            Products products = productsMapper.selectById(fav.getProductId());
            //由商品id得到类型，从而得到类型名称
            Categories categories = categoriesMapper.selectById(products.getCategoryId());
            favoritesVo.setCategoryName(categories.getCategoryName());

            BeanUtil.copyProperties(products, favoritesVo);

            return favoritesVo;
        }).collect(Collectors.toList());
        return favoritesVos;
    }

    //加入收藏夹
    @Override
    public void add(Integer userId, Integer productId) {
        // 查询是否已存在该收藏
        LambdaQueryWrapper<Favorites> wrapper = Wrappers.lambdaQuery(Favorites.class);
        wrapper.eq(Favorites::getProductId, productId);
        wrapper.eq(Favorites::getUserId, userId);
        Favorites favorites = favoritesMapper.selectOne(wrapper);

        if (favorites != null) {
            throw new RuntimeException("该商品已存在收藏夹中");
        } else {
            // 创建新的收藏对象
            favorites = new Favorites(); // 这里需要初始化对象
            favorites.setUserId(userId);
            favorites.setProductId(productId);
            favorites.setAddedDate(new Date()); // 设置当前时间
            favoritesMapper.insert(favorites);
        }
    }


    //删除收藏
    @Override
    public void remove(Integer userId, Integer productId) {
        LambdaQueryWrapper<Favorites> wrapper = Wrappers.lambdaQuery(Favorites.class);
        wrapper.eq(Favorites::getProductId,productId);
        wrapper.eq(Favorites::getUserId,userId);
        favoritesMapper.delete(wrapper);
    }




}








